<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onListToolsHelpDesk', 'plgHelpDeskPurgeOldFiles_onListToolsHelpDesk' );
$_MAMBOTS->registerFunction( 'onDisplayToolHelpDesk', 'plgHelpDeskPurgeOldFiles_onDisplayToolHelpDesk' );

	/**
	 * Executes the plugin
	 * @return 
	 */
	function plgHelpDeskPurgeOldFiles_onListToolsHelpDesk( $row ) {
		$return = plgHelpDeskPurgeOldFiles::onListToolsHelpDesk( $row );
		return $return;
	}
	
	/**
	 * Executes the plugin
	 * @return 
	 */
	function plgHelpDeskPurgeOldFiles_onDisplayToolHelpDesk( $row ) {
		$return = plgHelpDeskPurgeOldFiles::onDisplayToolHelpDesk( $row );
		return $return;
	}

class plgHelpDeskPurgeOldFiles 
{
	/**
	 * Checks to make sure that this plugin is the one being called by the component
	 *
	 * @access public
	 * @return mixed Parameter value
	 * @since 1.5
	 */
	function _isMe( $row ) 
	{
		$element = 'purgeoldfiles';
		
		$success = false;
		if (is_object($row) && !empty($row->element) && $row->element == $element )
		{
			$success = true;
		}
		if (is_string($row) && $row == $element ) {
			$success = true;
		}
		
		return $success;
	}

	/**
	 * Tells Component that this is a valid tool
	 * @return unknown_type
	 */
	function onListToolsHelpDesk( $row )
	{
		$success = false;
		if (plgHelpDeskPurgeOldFiles::_isMe($row)) 
		{
			$success = true;
		}
		return $success;	
	}

	/**
	 * Allows the plugin to prevent itself from being run for whatever reason
	 * @param $row
	 * @return unknown_type
	 */
	function onBeforeDisplayToolHelpDesk( $row ) 
	{
		$success = null;
		if (plgHelpDeskPurgeOldFiles::_isMe($row)) 
		{
			$success = true;
		}
		return $success;		
	}

	/**
	 * This displays the contents of the plugin
	 * @param $row
	 * @return unknown_type
	 */
	function onDisplayToolHelpDesk( $row ) 
	{
		$success = false;
		if (!plgHelpDeskPurgeOldFiles::_isMe($row)) 
		{
			return $success;
		}
		
		// Display the summary
			echo plgHelpDeskPurgeOldFiles::_onRenderSummary();

		// Display the results after submission
			echo plgHelpDeskPurgeOldFiles::_onRenderSubmit();
			
		return $success;		
	}
		
	/**
	 * 
	 * @return unknown_type
	 */
	function _onRenderSubmit() 
	{
		$mainframe 		= HelpDeskConfig::getApplication();
		$success = false;
		$html = "";
		
		// Check for request forgeries
		// and that form has been submitted
			if (!HelpDeskRequest::checkToken()) {
				return $success;
			}
			
			$response = plgHelpDeskPurgeOldFiles::_executeTool();
										
			$html .= '<table class="userlist">';
				$html .= '
	                <tr>
	                  <td>
						<p>
							</strong>'.HelpDeskText::_( $response ).'</strong>'
						.'
						</p>
					  </td>
	                </tr>
				';
			$html .= '</table>';
			
		return $html;
	}
	
	/**
	 * 
	 * @param $row
	 * @return unknown_type
	 */
	function _onRenderSummary() 
	{
		if (!defined('_HELPDESK_PLG_HELPDESK_PURGEOLDFILES_DESC'))
		{
			define('_HELPDESK_PLG_HELPDESK_PURGEOLDFILES_DESC', 'This plugin purges old attachments from your filesystem.');
		}
		$success = false;
	
		$html = "";
		
		$html .= '<div class="note">';
		$html .= '<table>';
			$html .= '
                <tr>
                  <td>
					<p>
				  	'.HelpDeskText::_( 'PLG_HELPDESK_PURGEOLDFILES_DESC' )
		            .'&nbsp;
		            </p>
				  </td>
                </tr>
			';
		$html .= '</table>';
		$html .= '</div>';
		
		return $html;
	}

	/**
	 * 
	 * @return 
	 */
	function _executeTool() 
	{
		$success = false;
		// select all physicalnames from db where date is older than 30 days
		$oldfiles = HelpDeskHelperHelpDesk::getOldTicketAttachments( "30" );
		$file = new HelpDeskFile();
		$dir = $file->getDirectory();
		$delete_array = array();
		
		if (count($oldfiles) < 1) {
			$success = HelpDeskText::_('No Files to Delete');
		}
		
		// foreach one, if file exists, delete it
		for ($i=0; $i<count($oldfiles); $i++) {
			$thisfile = $oldfiles[$i];
			if ($fileexists = HelpDeskFiles::exists( $dir.DS.$thisfile->physicalname)) {
				$delete_array[] = $dir.DS.$thisfile->physicalname;	
			}
		}
		
		if ( $delete = HelpDeskFiles::delete( $delete_array ) ) { $success = HelpDeskText::_( 'Old Files Deleted' ); }
		
		return $success;
	}	
	
}